<?php
if (!defined('__COLA_DIR__')) exit;

/**
 * 评论提交组件
 *
 * @author edikud
 * @package Widget
 * @copyright Copyright (c) 2019 Cola (http://www.mcooo.com)
 * @license GNU General Public License 2.0
 */
class Widget_Feedback extends Widget_Abstract_Comments implements Widget_Interface_Do
{
    /**
     * 初始化函数
     *
     * @access public
     * @return void
     * @throws Widget_Exception
     */
    public function action()
    {
        if (!$this->user->hasLogin()) {
			$this->result->set(403, _t('请登录.'));
        }
		/** 内容对象 */ 
        $contents = Router::match($this->request->permalink);

        /** 判断内容是否存在 */
        if (false !== $contents && $contents instanceof Widget_Abstract_Contents && $contents->have() && $contents->is('single')) {
			
            /** 评论关闭 */
            if (!$contents->allow('comment')) {
				$this->result->set(403, _t('对不起,此内容的反馈被禁止.'));
            }
			
            /** 检查来源 */
            if ($this->options->commentsCheckReferer) {
                $referer = $this->request->getReferer();
                if (empty($referer)) {
					$this->result->set(403, _t('对不起,评论来源页错误.'));
                }
                $refererPart = parse_url($referer);
                $currentPart = parse_url($contents->permalink);
                if ($refererPart['host'] != $currentPart['host'] || 0 !== strpos($refererPart['path'], $currentPart['path'])) {
                    $this->result->set(403, _t('对不起,评论来源页错误.'));
                }
            }
			
            /** 检查uid评论间隔 */
            if (!$this->user->pass('editor', true) && $contents->uid != $this->user->uid && $this->options->commentsPostIntervalEnable) {
				
                $latestComment = $this->db->select('created')
				    ->from('table.comments')
					->where('uid = ?', $this->user->uid)
					->order('created', 'DESC')
					->limit(1)
					->fetchRow();
					
                if ($latestComment && ($this->options->time - $latestComment['created'] > 0 && $this->options->time - $latestComment['created'] < $this->options->commentsPostInterval)) {
					$this->result->set(403, _t('对不起, 您的发言过于频繁, 请稍侯再次发布.'));
                }
            }

			$comment = array(
				'cid' => $contents->cid, 
				'created' => $this->options->time, 
				'ip' => $this->request->longIp(), 
				'uid' => $this->user->uid, 
				'text' => $this->request->text, 
				'ownerId' => $contents->author->uid
			);
			
            /** 判断父节点 */
            if ($parentId = $this->request->filter('int')->get('parent')) {
                if ($this->options->commentsThreaded && ($parent = $this->db->select('coid', 'cid')->from('table.comments')->where('coid = ?', $parentId)->fetchRow()) && $contents->cid == $parent['cid']) {
                    $comment['parent'] = $parentId;
                } else {
					$this->result->set(403, _t('父级评论不存在.'));
                }
            }
            if (empty($comment['text'])) {
				$this->result->set(403, _t('必须填写评论内容.'));
            }

            /** 评论接口 */
			//HOOK $comment | PLUG_Comment($comment, $contents, $this);
			
			$commentId = $this->widget('Widget_Comments_Edit')->insertComment($comment);
            $this->select()
			    ->where('coid = ?', $commentId)
                ->limit(1)
				->fetchRow(array($this, 'push'));

            /** 评论完成接口 */
			//HOOK PLUG_Comment_Success($this);

			$this->result->set(200, _t('评论成功.'), array('location'=> Common::backUrl('#' . $this->theId)));
        } else {
			$this->result->set(404, _t('请求的资源不存在'));
        }
    }
}